/* eslint-disable no-unused-vars,no-func-assign */
(function (window, doc) {
  /**
   * 事件绑定兼容方法
   * @param obj ： 节点
   * @param type ： 事件
   * @param handle ： 函数
   * @param boolean ： 不传默认冒泡，传true阻止冒泡
   */
  function addEvent (obj, type, handle, boolean) {
    if (obj.addEventListener) {
      boolean ? obj.addEventListener(type, function (e) {
        handle();
        e.stopPropagation();
      }) : obj.addEventListener(type, handle);
    } else {
      boolean ? obj.attachEvent('on' + type, function (e) {
        handle();
        e.cancelBubble = true;
      }) : obj.attachEvent('on' + type, handle);
    }
  }

  /**
   * @param selector
   * @param isArr
   * @return {*}
   */
  function getEle (selector, isArr) {
    if (!selector) return;
    if (selector[0] === '#') {
      return document.getElementById(selector.substr(1));
    } else if (selector[0] === '.') {
      return isArr ? document.getElementsByClassName(selector.substr(1)) : document.getElementsByClassName(selector.substr(1))[0];
    } else {
      return isArr ? document.getElementsByTagName(selector) : document.getElementsByTagName(selector)[0];
    }
  }

  HTMLDocument.prototype._getElementByClassName = function (eleName) {
    var res = [];
    f(this, eleName);

    function f (parent, eleName) {
      for (var i = 0; i < parent.childNodes.length; i++) {
        if (parent.childNodes[i].className === eleName) {
          res.push(parent.childNodes[i]);
          if (parent.childNodes[i].childNodes.length > 0) {
            f(parent.childNodes[i], eleName);
          }
        } else {
          f(parent.childNodes[i], eleName);
        }
      }
    }

    return res;
  };

  /***
   * @param dom {object} dom对象
   * @param attrName {string} 属性名
   * @param attrValue {string} 添加的属性值
   * @desc 可以添加dom对象上的class 和 style
   */
  function addAttr (dom, attrName, attrValue) {
    var originAttrValue = dom.getAttribute(attrName);
    console.log(originAttrValue);
    dom.setAttribute(attrName, originAttrValue + (attrName === 'class' ? '' : ';') + attrValue);
  }

  /***
   * @param dom :节点
   * @param attrName :属性名
   * @param attrValue :属性值
   * @return {string}
   */
  function attr (dom, attrName, attrValue) {
    if (!attrValue) {
      return dom.getAttribute(attrName);
    } else {
      dom.setAttribute(attrName, attrValue);
    }
  }

  /***
   * @param dom
   * @param cssJson
   */
  function style (dom, cssJson) {
    dom.style.cssText = Object.keys(cssJson).reduce(function (previousValue, currentValue) {
      return previousValue + currentValue + ':' + cssJson[currentValue] + ';';
    }, '');
  }

  /***
   *
   * @param dom
   * @param propName
   * @return {*}
   */
  function getStyle (dom, propName) {
    if (getComputedStyle) {
      getStyle = function (dom, propName) {
        return getComputedStyle(dom)[propName];
      };
      return getComputedStyle(dom)[propName];
    } else {
      getStyle = function (dom, propName) {
        return dom.currentStyle()[propName];
      };
      return dom.currentStyle()[propName];
    }
  }
})(window, document);
